package wsp.dailymarket.deals;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.sql.DataSource;

/**
 * Controller for the category view on the deals list page
 * @author Matt
 */
@ManagedBean(name="catView")
@RequestScoped
public class CategoryViewBean {

	private ArrayList<EditableCategory> catList;
	@Resource(name="jdbc/dmderby")
	private DataSource ds;
	private EditableCategory viewCat;

	public CategoryViewBean() {
		catList = new ArrayList<EditableCategory>();
		viewCat = new EditableCategory();
	}

	public ArrayList<EditableCategory> getCategoryList() {
		if (catList.isEmpty()) {
			this.reloadCategories();
		}
		return catList;
	}

	private void reloadCategories() {
		catList.clear();
		Connection conn = null;
		try {
			try {
				conn = ds.getConnection();
				Statement stmt = conn.createStatement();
				String sql = "SELECT child.*, parent.CATEGORY_NAME AS PARENT_NAME FROM app.categories AS child LEFT OUTER JOIN app.categories AS parent ON child.PARENT = parent.ID WHERE child.PARENT = " + this.viewCat.getID();
				ResultSet rs = stmt.executeQuery(sql);
				while (rs.next()) {
					EditableCategory newCat = new EditableCategory();
					newCat.setID(rs.getInt("ID"));
					newCat.setName(rs.getString("CATEGORY_NAME"));
					newCat.setDesc(rs.getString("DESCRIPTION"));
					newCat.setParentID(rs.getInt("PARENT"));
					newCat.setParentName(rs.getString("PARENT_NAME"));
					catList.add(newCat);
				}
			} finally {
				if (conn != null) {
					conn.close();
				}
			}
		} catch (SQLException ex) {
			ex.printStackTrace(System.err);
			FacesMessage msg = new FacesMessage();
			msg.setSeverity(FacesMessage.SEVERITY_FATAL);
			msg.setSummary("Database Error!");
			FacesContext.getCurrentInstance().addMessage(null, msg);
		}
	}

	public void setViewCategoryID(int newValue) {
		if (newValue == 0) {
			this.viewCat = new EditableCategory();
			this.reloadCategories();
			return;
		}

		Connection conn = null;
		try {
			try {
				conn = ds.getConnection();
				Statement stmt = conn.createStatement();
				ResultSet rs = stmt.executeQuery("SELECT child.*, parent.CATEGORY_NAME AS PARENT_NAME FROM app.categories AS child LEFT OUTER JOIN app.categories AS parent ON child.PARENT = parent.ID WHERE child.ID=" + newValue);
				if (rs.next()) {
					this.viewCat = new EditableCategory();
					this.viewCat.setID(newValue);
					this.viewCat.setName(rs.getString("CATEGORY_NAME"));
					this.viewCat.setDesc(rs.getString("DESCRIPTION"));
					this.viewCat.setParentID(rs.getInt("PARENT"));
					this.viewCat.setParentName(rs.getString("PARENT_NAME"));
					this.reloadCategories();
				} else {
					FacesMessage msg = new FacesMessage();
					msg.setSeverity(FacesMessage.SEVERITY_ERROR);
					msg.setSummary("Category does not exist!");
					FacesContext.getCurrentInstance().addMessage(null, msg);
				}
			} finally {
				if (conn != null) {
					conn.close();
				}
			}
		} catch (SQLException ex) {
			ex.printStackTrace(System.err);
			FacesMessage msg = new FacesMessage();
			msg.setSeverity(FacesMessage.SEVERITY_FATAL);
			msg.setSummary("Database Error!");
			FacesContext.getCurrentInstance().addMessage(null, msg);
		}
	}

	public int getViewCategoryID() {
		return this.viewCat.getID();
	}

	public void setViewCategory(EditableCategory newValue) {
		this.viewCat = newValue;
	}

	public String getViewCategoryTitle() {
		return this.viewCat.getID() > 0 ? this.viewCat.getName() : "Categories";
	}

	public EditableCategory getViewCategory() {
		return this.viewCat;
	}
}
